<?php


namespace app\admin\controller;


use app\common\model\AdPlayHistory;
use app\common\model\UserIncome;
use app\common\model\WechatShareLink;
use think\Controller;
use app\user\model\User as UserModel;
class Chart extends Controller
{

//    protected $middleware = [
//        'app\http\middleware\checkAdminToken::class'
//    ];
    //
    public function index()
    {
        $user_count = UserModel::count('id');
        //
        $new_user = UserModel::where([
            ['add_time', 'between', today()]
        ])->count('id');

        $vip_count = UserModel::where([
            ['vip','=',1],
        ])->count('id');
        $total_vip_user = UserModel::where([
            ['vip','=',1],
            ['add_time', 'between', today()]
        ])->count('id');

        //今天播放量
        $ad_today_count = AdPlayHistory::where([
            ['add_time', 'between', today()]
            ]
        )->count('id');

        //最新的ecpm
        $yesterday_ecpm = \app\common\model\Ecpm::yesterdayEcpm();

        $ad_count = AdPlayHistory::count('id');

        $yesterday = yesterday();
        $today = today();

        $today_total_movie_count = AdPlayHistory:: where([
            ['mini_app_code', '=', 'movie'],
            ['add_time', 'BETWEEN', $today]
        ])->count('id');

        $today_total_test_count = AdPlayHistory:: where([
            ['mini_app_code', '=', 'test'],
            ['add_time', 'BETWEEN', $today]
        ])->count('id');

        $yesterday_total_movie_count = UserIncome:: where([
            ['mini_app_code', '=', 'movie'],
            ['add_time', 'BETWEEN', $today]
        ])->sum('views');

        $yesterday_total_test_count = UserIncome:: where([
            ['mini_app_code', '=', 'test'],
            ['add_time', 'BETWEEN', $today]
        ])->sum('views');

        $today_total_count = $today_total_movie_count + $today_total_test_count;

        $yesterday_total_count = $yesterday_total_movie_count + $yesterday_total_test_count;

        //昨日总收益
        $yesterday_movie_income = UserIncome:: where([
            ['mini_app_code', '=', 'movie'],
            ['add_time', 'BETWEEN', $today]
        ])->sum('money');

        $yesterday_test_income = UserIncome:: where([
            ['mini_app_code', '=', 'test'],
            ['add_time', 'BETWEEN', $today]
        ])->sum('money');

        $yesterday_tatal_income = $yesterday_movie_income + $yesterday_test_income;

        //今日链接消耗量
        $total_url_test_count = WechatShareLink:: where([
            ['mini_app_code', '=', 'test'],
            ['add_time', 'BETWEEN', $today]
        ])->count('id');

        $total_url_movie_count = WechatShareLink:: where([
            ['mini_app_code', '=', 'moive'],
            ['add_time', 'BETWEEN', $today]
        ])->count('id');

        $total_url_all_count = $total_url_test_count + $total_url_movie_count;

        $data = [
            'user' => [
                'count' => $user_count,
                'today' => $new_user
            ],
            'vip' => [
                'count' => $vip_count,
                'today' => $total_vip_user
            ],
            'view' => [
                'count' => $ad_count,
                'today' => $ad_today_count
            ],
            'ecpm' => [
                'avg' => $yesterday_ecpm,
                'today' => $yesterday_ecpm
            ],
            'url' => [
                'test' => $total_url_test_count,
                'movie' => $total_url_movie_count,
                'count' => $total_url_all_count,
            ],
            'today_count' => [
                    ['name'=>'今日总播放量','value'=>$today_total_count],
                    ['name'=>'视频总播放量','value'=>$today_total_movie_count],
                    ['name'=>'测试总播放量','value'=>$today_total_test_count],

            ],
            'yesterday_total_count' => [
                    ['name'=>'昨日总播放量','value'=>$yesterday_total_count],
                    ['name'=>'视频播放量','value'=>$yesterday_total_movie_count],
                    ['name'=>'测试总播放量','value'=>$yesterday_total_test_count],
            ],
            'yesterday_income' => [
                ['name'=>'昨日总收益','value'=>$yesterday_tatal_income],
                ['name'=>'昨日视频收益','value'=>$yesterday_movie_income],
                ['name'=>'昨日测试收益','value'=>$yesterday_test_income],
                ]
        ];

        $this->result($data, 200);

    }

}
